<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/**
 * @package	
 */
class HelpDeskViewCategories extends HelpDeskView 
{
	/**
	 * (non-PHPdoc)
	 * @see v1_00/com_helpdesk/site/HelpDeskView#display($tpl)
	 */
	function display($tpl=null) 
	{
		$layout = $this->getLayout();
		switch(strtolower($layout))
		{
			case "define":
				$this->_define($tpl);
			  break;
			case "view":
			case "form":
				$this->_form($tpl);
			  break;
			case "default":
			default:
				$this->_default($tpl);
			  break;
		}
	}

	/**
	 * 
	 * @param $tpl
	 * @return unknown_type
	 */
	function _default($tpl=null) 
	{		
		$mainframe	= HelpDeskConfig::getApplication();
		$user 		= HelpDeskConfig::getUser();
		$database 	= HelpDeskConfig::getDBO();
		$model 		= $this->getModel();
		$app 		= HelpDeskConfig::getApplication();

		// check config		
			$config = &HelpDeskConfig::getInstance();
			$this->assignRef( 'config', $config );
			
        // Get data from the model
	        $items = $model->getData();
			for ($i=0; $i<count($items); $i++) 
			{
				$parent = HelpDeskHelperCategory::getInstance( $items[$i]->parentid );			
				$items[$i]->parent_title = $parent->title;
				
				$managers = HelpDeskHelperCategory::getManagers( $items[$i]->id );
				$list = '';
				for ($m=0; $m<count($managers); $m++) {
					$man = $managers[$m];
					$list .= $man->username." [".$man->id."]<br/>";
				}
				$items[$i]->managers_num 	= count($managers);
				$items[$i]->managers 		= $list;
			}
			$this->assignRef( 'items', $items );

		// filterlists
			$javascript = 'onchange="document.adminForm.submit();"';
			$filterLists = array();
			$filterLists[] = HelpDeskHelperCategory::getSelectList( $config->getFieldname( 'parentid' ), $model->getField( $config->getFieldname( 'parentid' ) ), $javascript );
				$types = array();
				$types[] = HelpDeskHTML::selectOption( '', '-'.HelpDeskText::_( 'Select State' ).'-' );
				$types[] = HelpDeskHTML::selectOption( '0', HelpDeskText::_( 'Unpublished' ) );
				$types[] = HelpDeskHTML::selectOption( '1', HelpDeskText::_( 'Published' ) );
			$filterLists[] = HelpDeskHTML::genericList( $types, $config->getFieldname( 'publishedid' ), $javascript, 'value','text', $model->getField( $config->getFieldname( 'publishedid' ) ) ); 
			$this->assignRef('filterLists', $filterLists );
			
		// set search
			$searchterm = $model->getField( $config->getFieldname( 'search' ) );
			$search = "<input type='text' name='".$config->getFieldname( 'search' )."' value='$searchterm' class='text_area' onchange='submitForm(\"".HelpDeskRequest::getVar( 'task' )."\")' />";
			$this->assignRef( 'search' , $search );
			
		// pagenavigation
			$this->assignRef( 'order' , $model->getOrder() );
			$this->assignRef( 'order_dir' , $model->getOrderDir() );
			$pagination = $model->getPagination();    
			$this->assignRef( 'pagination' , $pagination);
		
		// form
			$formAction = "index2.php";
			$this->assignRef( 'formAction' , $formAction );
			$validate = josSpoofValue();
			$formValidate = "<input type='hidden' name='{$validate}' value='1' />"; 
			$this->assignRef( 'formValidate' , $formValidate );
			$this->assignRef( 'formOption' , HelpDeskRequest::getVar( 'option' ) );
			$this->assignRef( 'formController' , $this->_name );
			$this->assignRef( 'formId' , $model->_id );
		
		parent::display($tpl);
		
	}

	
	/**
	 * 
	 * @param $tpl
	 * @return unknown_type
	 */	
	function _form($tpl)
	{
		$mainframe	= HelpDeskConfig::getApplication();
		$user 		= HelpDeskConfig::getUser();
		$database 	= HelpDeskConfig::getDBO();
		$model 		= $this->getModel();
		$app 		= HelpDeskConfig::getApplication();
		
		// check config		
			$config = &HelpDeskConfig::getInstance();
			$this->assignRef( 'config', $config );

        // Get data from the model
			$row = $model->getTable('category');
			$row->load( (int) $model->getId() );
			if ($isNew = ($row->id < 1)) { 
				// used for plugins
					$act = 'Edit';
					$pagetitle  = HelpDeskText::_( 'Create Category' );

			} else {
				// used for plugins
				$act = 'Edit';
				$pagetitle  = HelpDeskText::_( $act.' Category' );
			}
			
		// lists
			$select_parentid = HelpDeskHelperCategory::getSelectList( 'parentid', $row->parentid );
				$types = array();
				$types[] = HelpDeskHTML::selectOption( '', '-'.HelpDeskText::_( 'Select State' ).'-' );
				$types[] = HelpDeskHTML::selectOption( '0', HelpDeskText::_( 'Unpublished' ) );
				$types[] = HelpDeskHTML::selectOption( '1', HelpDeskText::_( 'Published' ) );
			$select_published = HelpDeskHTML::genericList( $types, 'published', '', 'value','text', $row->published ); 
			$this->assignRef('select_parentid', $select_parentid );
			$this->assignRef('select_published', $select_published );

        // Get plugins
        	$filtered_sliders = array();
        	$filtered_tabs = array();
        	$filtered = array();
        	$items = HelpDeskHelperTools::getPlugins();        	
				// if necessary, do things to each item
				for ($i=0; $i < count($items); $i++) {
					$item = &$items[$i];
					// Check if they have an event
					if (HelpDeskHelperTools::hasEvent( $item, 'onEditCategorySliders' )) {
						// add item to filtered array
						$filtered_sliders[] = $item;
					}
					
				}
			$items_sliders = $filtered_sliders;
	        $this->assignRef( 'items_sliders', $items_sliders );
	        
		// Add pane
			$sliders = HelpDeskPane::getInstance( 'sliders' );		
			$this->assignRef('sliders', $sliders);
			
		// set the required image
			$required = new stdClass();
			$required->text = HelpDeskText::_( 'Required' );
			$required->image = "<img src='".HelpDeskUrl::root()."/components/"._THISCOMPONENT."/images/required_16.png' alt='{$required->text}'>";
			$dispatcher = HelpDeskHelperPlugin::getInstance();
			$dispatcher->trigger( 'onSetRequiredImage', array( $required ) );
			$this->assignRef('required', $required );
						
		// Assign variables
			$this->assignRef('pagetitle', $pagetitle );
			$this->assignRef('user', $user );
	        $this->assignRef('row', $row);
	        
		// form
			$formAction = "index2.php";
			$this->assignRef( 'formAction' , $formAction );
			$validate = josSpoofValue();
			$formValidate = "<input type='hidden' name='{$validate}' value='1' />"; 
			$this->assignRef( 'formValidate' , $formValidate );
			$this->assignRef( 'formOption' , HelpDeskRequest::getVar( 'option' ) );
			$this->assignRef( 'formController' , $this->_name );
			$this->assignRef( 'formId' , $model->_id );

		// fire plugins and display
			$dispatcher	   = HelpDeskHelperPlugin::getInstance();
			$beforeView = $dispatcher->trigger( 'onBefore'.$act.'Category', array( &$row, $user ) );
			if (in_array(false, $beforeView, true)) {
				HelpDeskError::raiseError(500, $row->getError());
				return false;
			}
		
			parent::display($tpl);

			$dispatcher->trigger( 'onAfter'.$act.'Category', array( $row, $user ) );
	}
	
	/**
	 * 
	 * @param $tpl
	 * @return unknown_type
	 */	
	function _define($tpl)
	{
		$mainframe	= HelpDeskConfig::getApplication();
		$user 		= HelpDeskConfig::getUser();
		$database 	= HelpDeskConfig::getDBO();
		$model 		= $this->getModel();
		$app 		= HelpDeskConfig::getApplication();
		
		// check config		
			$config = &HelpDeskConfig::getInstance();
			$this->assignRef( 'config', $config );

        // Get data from the model
			$row = $model->getTable('category');
			$id = HelpDeskRequest::getVar( 'id' );
			$model->setId( $id );
			$row->load( $model->_id );
				$parent = HelpDeskHelperCategory::getInstance( $row->parentid );			
				$row->parent_title = isset( $parent->title ) ? $parent->title : '';
							
		// act & pagetitle
			$act = 'Define';
			$pagetitle  = HelpDeskText::_( $act.' Category' );
						
        // Get data from the model
        	$imodel = HelpDeskModel::getInstance( 'users', 'HelpDeskModel' );
	        $items = $imodel->getData();
			for ($i=0; $i<count($items); $i++) 
			{

			}
			$this->assignRef( 'items', $items );	
		// pagenavigation
			$this->assignRef( 'order' , $imodel->getOrder() );
			$this->assignRef( 'order_dir' , $imodel->getOrderDir() );
			$pagination = $imodel->getPagination();    
			$this->assignRef( 'pagination' , $pagination);
			
		// set the required image
			$required = new stdClass();
			$required->text = HelpDeskText::_( 'Required' );
			$required->image = "<img src='".HelpDeskUrl::root()."/components/"._THISCOMPONENT."/images/required_16.png' alt='{$required->text}'>";
			$dispatcher = HelpDeskHelperPlugin::getInstance();
			$dispatcher->trigger( 'onSetRequiredImage', array( $required ) );
			$this->assignRef('required', $required );
						
		// Assign variables
			$this->assignRef('pagetitle', $pagetitle );
			$this->assignRef('user', $user );
	        $this->assignRef('row', $row);
	        
		// form
			$formAction = "index2.php";
			$this->assignRef( 'formAction' , $formAction );
			$validate = josSpoofValue();
			$formValidate = "<input type='hidden' name='{$validate}' value='1' />"; 
			$this->assignRef( 'formValidate' , $formValidate );
			$this->assignRef( 'formOption' , HelpDeskRequest::getVar( 'option' ) );
			$this->assignRef( 'formController' , $this->_name );
			$this->assignRef( 'formId' , $model->_id );
			$this->assignRef( 'formTask' , HelpDeskRequest::getVar( 'task' ) );

		// fire plugins and display
			$dispatcher	   = HelpDeskHelperPlugin::getInstance();
			$beforeView = $dispatcher->trigger( 'onBefore'.$act.'Category', array( &$row, $user ) );
			if (in_array(false, $beforeView, true)) {
				HelpDeskError::raiseError(500, $row->getError());
				return false;
			}
		
			parent::display($tpl);

			$dispatcher->trigger( 'onAfter'.$act.'Category', array( $row, $user ) );
	}
	
}